<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" label-width="220px" :model="employeeForm" :rules="employeeRules">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="employeeForm.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <!-- 工号是系统生成的 不能修改 -->
                <el-input v-model="employeeForm.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="employeeForm.mobile"
                  :disabled="!!$route.params.id"
                  size="mini"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 会单独封装-->
                <!-- class="inputW" 表示样式会给到 SelectTree中 template的第一层组件 -->
                <SelectTree v-model="employeeForm.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="employeeForm.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="employeeForm.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="employeeForm.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <image-upload v-model="employeeForm.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left: 220px">
              <el-button size="mini" type="primary" @click="saveUser">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import SelectTree from './components/select-tree.vue'
import ImageUpload from './components/image-upload.vue'
import {
  addEmployeeService,
  getEmployeeInfoService,
  updateEmployeeService
} from '@/api/employee.js'
export default {
  components: { SelectTree, ImageUpload },
  data() {
    return {
      employeeForm: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentId: null,
        timeOfEntry: '',
        correctionTime: '',
        staffPhoto: ''
      },
      employeeRules: {
        username: [
          { required: true, message: '员工姓名不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 4,
            message: '长度不在1~4位字符之间',
            trigger: ['change', 'blur']
          }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式错误',
            trigger: ['blur', 'change'] // 输入时和失去焦点都校验
          }
        ],
        departmentId: [{ required: true, message: '部门不能为空', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '请选择聘用形式', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' }],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' },
          {
            trigger: 'change',
            validator: (rule, value, callback) => {
              if (this.employeeForm.timeOfEntry) {
                if (new Date(this.employeeForm.timeOfEntry) > new Date(value)) {
                  callback(new Error('转正时间不能小于入职时间'))
                  return
                }
              }
              callback()
            }
          }
        ]
      }
    }
  },
  async created() {
    // 如何获取路由参数中的id
    // console.log(this.$route.params.id);
    this.$route.params.id && this.getEmployeeInfo()
  },
  methods: {
    saveUser() {
      this.$refs.userForm.validate(async (isOk) => {
        if (isOk) {
          // 校验成功
          // 判断是否为编辑模式
          if (this.$route.params.id) {
            // 编辑模式
            await updateEmployeeService(this.employeeForm)
            this.$message.success('更新员工信息成功')
          } else {
            // 新增模式
            await addEmployeeService(this.employeeForm)
            this.$message.success('新增员工成功')
          }
          this.$router.push('/employee')
        }
      })
    },
    async getEmployeeInfo() {
      const id = this.$route.params.id
      const res = await getEmployeeInfoService(id)
      this.employeeForm = res.data
    }
  }
}
</script>

<style scoped lang="scss">
.edit-form {
  background: #fff;
  padding: 20px;
  .inputW {
    width: 380px;
  }
}
</style>
